#include "CCallFuncInstant.h"

CCallFuncInstant* CCallFuncInstant::create(CCObject* pSelectorTarget, SEL_CallFuncO pCallFuncO, CCObject* pObj) 
{
	CCallFuncInstant *pRet = new CCallFuncInstant();

	if (pRet && pRet->initWithTarget(pSelectorTarget, pObj)) {
		pRet->m_pCallFuncO = pCallFuncO;
		pRet->autorelease();
		return pRet;
	}

	CC_SAFE_DELETE(pRet);
	return NULL;
}

bool CCallFuncInstant::initWithTarget(CCObject* pSelectorTarget, CCObject* pObj) {
	m_pSelectorTarget = pSelectorTarget;
	m_pObject = pObj;
	return true;
}

CCObject * CCallFuncInstant::copyWithZone(CCZone *pZone) {
	CCZone* pNewZone = NULL;
	CCallFuncInstant* pRet = NULL;

	if (pZone && pZone->m_pCopyObject) {
		//in case of being called at sub class
		pRet = (CCallFuncInstant*) (pZone->m_pCopyObject);
	} else {
		pRet = new CCallFuncInstant();
		pZone = pNewZone = new CCZone(pRet);
	}

	CCActionInstant::copyWithZone(pZone);
	pRet->initWithTarget(m_pSelectorTarget);
	pRet->m_pCallFuncO = m_pCallFuncO;
	pRet->m_pObject = m_pObject;

	CC_SAFE_DELETE(pNewZone);
	return pRet;
}

void CCallFuncInstant::update(float time) {
	CC_UNUSED_PARAM(time);
	execute();
}

void CCallFuncInstant::execute() {
	if (m_pCallFuncO) {
		(m_pSelectorTarget->*m_pCallFuncO)(m_pObject);
	}
}